<?php

class Admin_DistrictController extends Zend_Controller_Action
{	
    private $_params;
	private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();
        
        $this->_acl         = new My_Acl();
        $this->view->acl    = $this->_acl;
        
        $this->_auth        = $this->_acl->checkAllowed();
        $this->view->auth   = $this->_auth;
    }
    public function indexAction(){       
        $modelDistrict              = new Admin_Model_District();        
        $districtList               = $modelDistrict->getAllData()->toArray();        
        $this->view->districtList   = $districtList;
    }
    public function getdistrictAction(){        
        $provinceId = $this->_request->getParam('provinceId');
        $modelDistrict=new Admin_Model_District();
        $districtTemp=$modelDistrict->getDataByProvinceId($provinceId);        
        foreach($districtTemp as $district){
            $districtList[$district['districtId']]=$district['districtName'];
        }                
        echo json_encode($districtList);
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
    }
    public function exportAction(){        
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $modelProvince = new Admin_Model_Province();
                
        $workbook = My_Pear::getInstance_ExcelWriter();
        
        // sending HTTP headers
        $workbook->send($modelProvince->info(Zend_Db_Table_Abstract::NAME).'.xls');
        $workbook->setVersion(8);
                                   
        $provinceList=$modelProvince->getAllData();
        $cols=$modelProvince->info(Zend_Db_Table_Abstract::COLS);

        // Creating a worksheet
        $worksheet =& $workbook->addWorksheet($modelProvince->info(Zend_Db_Table_Abstract::NAME));
        $worksheet->setInputEncoding('utf-8');
        // The actual data
        $i=0;
        foreach($cols as $col){
            $worksheet->write(0, $i++, $col);
        }
        $i=1;
        foreach($provinceList as $pro){
            $worksheet->write($i, 0, $pro['provinceId']);
            $worksheet->write($i, 1, $pro['provinceName']);
            $worksheet->write($i, 2, $pro['typeName']);
            $worksheet->write($i++, 3, $pro['type']);            
        }               
        
        // Let's send the file
        $workbook->close();        
    }
    public function ajaxprovinceAction(){        
        $modelProvince=new Admin_Model_Province();
        $provinceTemp=$modelProvince->getAllData();
        foreach($provinceTemp as $province){
            $provinceList[$province['provinceId']]=$province['provinceName'];
        }        
        echo json_encode($provinceList);
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
    }
    public function addAction()
    {   
        $form = new Admin_Form_Company();
        $form->submit->setLabel('Thêm mới');
        $form->removeElement('website');
        $form->removeElement('address');
        $form->removeElement('tax');
        $form->removeElement('fax');
        $form->removeElement('mobile');
        $form->removeElement('logo');
        $form->removeElement('email');
        $form->removeElement('skype');
        $form->removeElement('yahoo');
        $form->removeElement('intro');
        
        $this->view->form = $form;
        if($this->getRequest()->isPost()){
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $addData['userId']    = $form->getValue('userId');
                $addData['fullname']      = $form->getValue('fullname');
                $addData['shortName']   = My_Service_String::shortenString($form->getValue('fullname'));
                $addData['contact']      = $form->getValue('contact');
                $addData['phone']      = $form->getValue('phone');
                
                $modelUser = new Admin_Model_User();
                $modelCompany = new Admin_Model_Company();
                $userRoleCheck = $modelUser->getDataByUserId($addData['userId']);
                $userFreeComCheck = $modelCompany->getDataByUserId($addData['userId']); 
                
                if(($userRoleCheck['role'] == 1 && !$userFreeComCheck) || $addData['userId'] == 0){                    
                    $comId = $modelCompany->addData($addData);
                    if($comId){
                        $this->_helper->redirector('index', 'company', 'admin');
                    }else{
                        $this->view->mess = "Đã có lỗi xảy ra. Vui lòng thử lại!";
                        $this->view->flag = "error";
                    }
                }
            }else{
                $form->populate($formData);
            }
        }
    }
    public function editAction(){
        $comId = (int)($this->_getParam('comId', 0));
        $modelCompany = new Admin_Model_Company();
        $companyCheck = $modelCompany->getDataByCompanyId($comId);
        $this->view->companyCheck = $companyCheck;
        $form = new Admin_Form_Company();
        $form->submit->setLabel('Lưu thay đổi');
        $this->view->form = $form;
        if($companyCheck){
            if($this->getRequest()->isPost()){
                $formData = $this->getRequest()->getPost();
                if($form->isValid($formData)){
                    $editData['comId']      = $form->getValue('comId');
                    $editData['userId']     = $form->getValue('userId');
                    $editData['fullname']   = $form->getValue('fullname');
                    $editData['shortName']  = My_Service_String::shortenString($form->getValue('fullname'));
                    $editData['contact']    = $form->getValue('contact');
                    $editData['phone']      = $form->getValue('phone');
                    $editData['fax']        = $form->getValue('fax');
                    $editData['mobile']     = $form->getValue('mobile');
                    $editData['email']      = $form->getValue('email');
                    $editData['address']    = $form->getValue('address');
                    $editData['tax']        = $form->getValue('tax');
                    $editData['website']    = $form->getValue('website');
                    $editData['skype']      = $form->getValue('skype');
                    $editData['yahoo']      = $form->getValue('yahoo');
                    $editData['intro']      = $form->getValue('intro');
                    
                    $upload = new Zend_File_Transfer();
                    $files = $upload->getFileInfo();
                    if($upload->getFileName(null,false)){
                        $upload->setDestination(PUBLIC_PATH.'/upload/cologo/');
                        $upload->addFilter('Rename', date("YmdHis-").$editData['comId'].".".My_Service_File::getExtension($upload->getFileName(null,false)) );
                        $upload->receive();
                        if($upload->getFileName(null,false)){
                            $editData['logo'] = '/upload/cologo/'.$upload->getFileName(null,false);
                        }else{
                            $editData['logo'] = "";
                        }
                    }else $editData['logo'] = $companyCheck['logo'];
                    $modelCompany->updateData($editData['comId'],$editData);
                    $this->_helper->redirector('index', 'company', 'admin');
                }else{
                    $form->populate($formData);
                }
            }else{
                $form->populate($companyCheck->toArray());
            }
        }else{
            $this->_helper->redirector('index', 'company', 'admin');
        }
    }
    public function deleteAction()
    {
        if($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){
                $comId = $this->getRequest()->getPost('comId');
                $modelCompany = new Admin_Model_Company();
                $modelCompany->deleteData($comId);
                // ghi log
            }
            $this->_helper->redirector('index', 'company', 'admin');
        }else{
            $id = $this->_getParam('comId', 0);
            $modelCompany = new Admin_Model_Company();
            $this->view->companyCheck = $modelCompany->getDataByCompanyId($id);
        }
    }
}